home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / Menu Controls / UDrawShapes.cp < prev    next >
Text File  |  1995-06-24  |  11KB  |  383 lines

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // UDrawShapes.cp
  3.  
  4. #ifndef __UDRAWSHAPES__
  5. #include "UDrawShapes.h"
  6. #endif
  7.  
  8. // DrawShapes
  9.  
  10. #ifndef __BETTERFEEDBACK__
  11. #include "BetterFeedback.h"
  12. #endif
  13.  
  14. #ifndef __HACKMENU__
  15. #include "HackMenu.h"
  16. #endif
  17.  
  18. #ifndef __PATTERNMENU__
  19. #include "PatternMenu.h"
  20. #endif
  21.  
  22. #ifndef __TOOLSMENU__
  23. #include "ToolsMenu.h"
  24. #endif
  25.  
  26. #ifndef __USHAPES__
  27. #include "UShapes.h"
  28. #endif
  29.  
  30. #ifndef __USHAPESDOCUMENT__
  31. #include "UShapesDocument.h"
  32. #endif
  33.  
  34. #ifndef __USHAPEVIEW__
  35. #include "UShapeView.h"
  36. #endif
  37.  
  38. #ifndef __UTEXTSHAPE__
  39. #include "UTextShape.h"
  40. #endif
  41.  
  42. #ifndef __UPICTSHAPE__
  43. #include "UPictShape.h"
  44. #endif
  45.  
  46. // MacApp
  47.  
  48. #ifndef __UMENUMGR__
  49. #include <UMenuMgr.h>
  50. #endif
  51.  
  52. #ifndef __UMEMORY__
  53. #include <UMemory.h>
  54. #endif
  55.  
  56. #ifndef __UVIEWSERVER__
  57. #include <UViewServer.h>
  58. #endif
  59.  
  60. #ifndef __UWINDOW__
  61. #include <UWindow.h>
  62. #endif
  63.  
  64. #ifndef __UMACAPPGLOBALS__
  65. #include "UMacAppGlobals.h"
  66. #endif
  67.  
  68. #ifndef __UMACAPPUTILITIES__
  69. #include "UMacAppUtilities.h"
  70. #endif
  71.  
  72. // Toolbox
  73.  
  74. #ifndef __SCRAP__
  75. #include <Scrap.h>
  76. #endif
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // Globals
  80.  
  81. TShapeApplication* gShapeApplication;
  82.  
  83. Boolean            gPasteReplacesSelection;        // Tells whether Paste should _replace_ the
  84.                                                 // existing selection, or instead simply add
  85.                                                 // new shapes without replacement.
  86.                                                 // Default: false
  87.                                                 // Change its value by using the "More Debug"
  88.                                                 // menu, obtainable by typing Cmd-D.
  89.  
  90. Boolean            gConstrainDrags;                // Whether dragging shapes with the mouse
  91.                                                 // should be constrained so that nothing
  92.                                                 // overlaps the view's borders.
  93.                                                 // Default: true
  94.                                                 // Change its value by using the "More Debug"
  95.                                                 // menu, obtainable by typing Cmd-D.
  96.  
  97. short            gStaggerCount;                    // for SimpleStagger
  98.  
  99. CCrsrHandle        gRainbowArrow;
  100.  
  101. Boolean            gBetterFeedback;                // true to invoke BetterFeedback routines
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // Resources
  105.  
  106. // Menu bars for color and for b&w systems
  107. #define kColorMenuBar        131
  108. #define kNonColorMenuBar    132
  109.  
  110. #define kRainbowArrow        140
  111.  
  112. #define kPickerPrompt        256
  113.  
  114. const OSType kDocRsrcKind        = 'DSTA';    // Resource type for document state
  115. const short  kDocStateID        = 1;        // Resource id for document state
  116.  
  117. // Windows
  118.  
  119. const short kShapeWindowRSRCID    = 1005;        // Windows used by documents in the application
  120.  
  121. const short kShapeViewRSRCID    = 1006;        // Resource id for the shape view template,
  122.                                             // used when printing & using template views
  123.  
  124. const short kToolsPaletteRSRCID = 1007;        // Resource id for the floating window with
  125.                                             // tools palette
  126.  
  127. const short kPatternsPaletteRSRCID = 1008;    // Resource id for the floating window with
  128.                                             // patterns palette
  129.  
  130. // Menus
  131. const short mTools            = 5;            // the Tools menu resource id
  132. const short mPatterns        = 6;            // the Patterns menu resource id
  133. const short mColor            = 7;            // the Color menu resource id
  134. const short mMoreDebug        = 8;            // Menu number for the "More Debug" menu
  135.  
  136.  
  137. // Command Numbers
  138.  
  139. #define cArrow    1100
  140. #define cBox    1101
  141. #define cCircle    1102
  142. #define cHBox    1103
  143. #define cTextShape    1104
  144.  
  145.  
  146. //========================================================================================
  147. // CLASS TShapeApplication
  148. //========================================================================================
  149. #undef Inherited
  150. #define Inherited TApplication
  151.  
  152. #pragma segment AInit
  153. MA_DEFINE_CLASS_M1(TShapeApplication, Inherited);
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // TShapeApplication Constructor
  157. //----------------------------------------------------------------------------------------
  158. #pragma segment AInit
  159.  
  160. TShapeApplication::TShapeApplication()
  161. {
  162.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  163.         fDisplayedMenus = kColorMenuBar;
  164.     else
  165.         fDisplayedMenus = kColorMenuBar;
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // TShapeApplication::IShapeApplication: 
  170. //----------------------------------------------------------------------------------------
  171. #pragma segment AInit
  172.  
  173. void TShapeApplication::IShapeApplication()
  174. {
  175.     this->IApplication(kDocType, kSignature);
  176.     gShapeApplication = this;
  177.  
  178.     // Suppress Linker dead-stripping of these classes 
  179.     macroDontDeadStrip(TShapeView);
  180.     macroDontDeadStrip(TToolsPalette);
  181.     macroDontDeadStrip(TPatternsPalette);
  182.     
  183.     macroDontDeadStrip(THackView);
  184.  
  185.     // Copy patterns into global array of patterns
  186.     InitPatternsMenu();
  187.  
  188.     // Now create the floating palette window
  189.     gToolsPaletteWindow = gViewServer->NewTemplateWindow(kToolsPaletteRSRCID, NULL);
  190.     FailNIL(gToolsPaletteWindow);
  191.  
  192.     // Now create the floating Patterns window
  193.     gPatternsWindow = gViewServer->NewTemplateWindow(kPatternsPaletteRSRCID, NULL);
  194.     FailNIL(gPatternsWindow);
  195.  
  196.     // Install our custom tools menu
  197.     TToolsMenu* toolsMenu = new TToolsMenu;
  198.     toolsMenu->IToolsMenu(mTools);
  199.     gToolsMenu = toolsMenu;
  200.  
  201.     // Install our custom pattern menu
  202.     TPatternsMenu* patternsMenu = new TPatternsMenu;
  203.     patternsMenu->IPatternsMenu(mPatterns);
  204.     gPatternsMenu = patternsMenu;
  205.  
  206.     // Install our hack
  207.     THackMenu* controlMenu = new THackMenu;
  208.     controlMenu->IHackMenu(9, 1009);
  209.  
  210.     // Define the prototype shapes
  211.     short id = 0;
  212.     CRect r(10, 10, 28, 30);
  213.     
  214.     TArrowTool* arrowTool = new TArrowTool;
  215.     arrowTool->IArrowTool(r, id);
  216.     SetShapeInArray(arrowTool);
  217.     
  218.     ++id;
  219.     OffsetRect(r, 0, 40);
  220.     TBox* box = new TBox;
  221.     box->IBox(r, id);
  222.     SetShapeInArray(box);
  223.  
  224.     ++id;
  225.     OffsetRect(r, 0, 40);
  226.     TCircle* circle = new TCircle;
  227.     circle->ICircle(r, id);
  228.     SetShapeInArray(circle);
  229.  
  230.     ++id;
  231.     OffsetRect(r, 0, 40);
  232.     THeavyBox* heavyBox = new THeavyBox;
  233.     heavyBox->IHeavyBox(r, id);
  234.     SetShapeInArray(heavyBox);
  235.  
  236.     ++id;
  237.     OffsetRect(r, 0, 40);
  238.     TTextShape* textShape = new TTextShape;
  239.     textShape->ITextShape(r, id);
  240.     SetShapeInArray(textShape);
  241.  
  242.     ++id;
  243.     OffsetRect(r, 0, 40);
  244.     TPictShape* pictShape = new TPictShape;
  245.     pictShape->IPictShape(r, id);
  246.     SetShapeInArray(pictShape);
  247.  
  248.     VPoint newSize(41, GetNumberOfShapes() * 40);
  249.     gToolsPaletteWindow->Resize(newSize, kDontInvalidate);
  250.     
  251.     gPasteReplacesSelection = false;
  252.     gConstrainDrags = true;
  253.     gStaggerCount = 0;
  254.  
  255.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  256.     {
  257.         // Unlike GetCursor, GetCCursor makes a copy of the color cursor resource.
  258.         // Therefore, you should make one call to GetCCursor and multiple calls to SetCCursor.
  259.         gRainbowArrow = GetCCursor(kRainbowArrow);
  260.         FailNIL(gRainbowArrow);
  261.     }
  262.     gBetterFeedback = kBetterFeedbackDesired;
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. // TShapeApplication::DoMakeDocument: 
  267. //----------------------------------------------------------------------------------------
  268. #pragma segment AOpen
  269.  
  270. TDocument* TShapeApplication::DoMakeDocument(CommandNumber /*itsCommandNumber*/,
  271.                                              TFile* itsFile)    // Override
  272. {
  273.     TShapeDocument* aShapeDocument = new TShapeDocument;
  274.     aShapeDocument->IShapeDocument(itsFile);
  275.  
  276.     return aShapeDocument;
  277. } // TShapeApplication::DoMakeDocument 
  278.  
  279. #if qDebug
  280. //----------------------------------------------------------------------------------------
  281. // TShapeApplication::DoCommandKeyEvent - This illustrates how to have a 'Command-key-only'
  282. //    command, i.e. a command which is NOT in a menu, but rather only available when the user
  283. //    types the Command key and another key concurrently.  In this example, the user presses
  284. //    'Command-D' (the D can be in upper or lower case) to request that the special "More
  285. //    Debug" menu be put up (or taken down if it was already up).
  286. //----------------------------------------------------------------------------------------
  287. #pragma segment ASelCommand
  288.  
  289. void TShapeApplication::DoCommandKeyEvent(TToolboxEvent* event)    // Override 
  290. {
  291.     if (UprChar(event->fCharacter) == 'D')
  292.         this->DoMenuCommand(cToggleMoreDebugMenu);
  293.     else
  294.         Inherited::DoCommandKeyEvent(event);
  295. } // TShapeApplication::DoCommandKeyEvent 
  296. #endif
  297.  
  298. #if qDebug
  299. //----------------------------------------------------------------------------------------
  300. // TShapeApplication::DoMenuCommand: 
  301. //----------------------------------------------------------------------------------------
  302. #pragma segment ASelCommand
  303.  
  304. void TShapeApplication::DoMenuCommand(CommandNumber aCommandNumber)    // Override 
  305. {
  306.     switch (aCommandNumber)
  307.     {
  308.         case cPasteReplacesSelection:
  309.             gPasteReplacesSelection = !gPasteReplacesSelection;
  310.             break;
  311.  
  312.         case cConstrainDrags:
  313.             gConstrainDrags = !gConstrainDrags;
  314.             break;
  315.  
  316.         case cToggleMoreDebugMenu:
  317.         {
  318.             if (GetMenuHandle(mMoreDebug) == NULL)        // menu not currently up - put it up
  319.                 InsertMenu(MAGetMenu(mMoreDebug), 0);
  320.             else                                        // menu currently up - take it down
  321.                 DeleteMenu(mMoreDebug);
  322.             InvalidateMenuBar();                        // Get it redrawn
  323.             break;
  324.         }
  325.  
  326.         default:
  327.             Inherited::DoMenuCommand(aCommandNumber);
  328.     }
  329. } // TShapeApplication::DoMenuCommand 
  330. #endif
  331.  
  332. #if qDebug
  333. //----------------------------------------------------------------------------------------
  334. // TShapeApplication::DoSetupMenus: 
  335. //----------------------------------------------------------------------------------------
  336. #pragma segment ARes
  337.  
  338. void TShapeApplication::DoSetupMenus()    // Override 
  339. {
  340.     Inherited::DoSetupMenus();
  341.     EnableCheck(cPasteReplacesSelection, true, gPasteReplacesSelection);
  342.     EnableCheck(cConstrainDrags, true, gConstrainDrags);
  343. }
  344. #endif
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // TShapeApplication::MakeViewForAlienClipboard: Launch a view to represent the data found
  348. //    in the Clipboard at application start-up time, or when switching back in, or when
  349. //    returning from a Desk Accessory.
  350. //----------------------------------------------------------------------------------------
  351. #pragma segment AClipboard
  352.  
  353. TView* TShapeApplication::MakeViewForAlienClipboard()    // Override 
  354. {
  355.     TView*    clipView = NULL;
  356.  
  357.     // Before doing anything else, make sure the scrap contains shapes
  358.     long offset;
  359.     long result;
  360.     result = GetScrap(NULL, kShapeClipType, &offset);
  361.     if (result <= 0)
  362.         return Inherited::MakeViewForAlienClipboard();
  363.  
  364.     // found my kind of data
  365.     MAVolatileInit(TShapeDocument*, clipShapeDoc, NULL);
  366.     FailInfo fi;
  367.     Try(fi)
  368.     {
  369.         clipShapeDoc = new TShapeDocument;
  370.         clipShapeDoc->IShapeDocument(NULL);
  371.         
  372.         clipView = clipShapeDoc->MakeClipView();
  373.         fi.Success();
  374.     }
  375.     else // Recover
  376.     {
  377.         clipShapeDoc = (TShapeDocument*) FreeIfObject(clipShapeDoc);
  378.         fi.ReSignal();
  379.     }
  380.     return clipView;
  381. }
  382.  
  383.